﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MSMQTest {
  public partial class Form1 : Form {

    MessageQueueTaskPool<FtpFile> messageQueuePool;
    private List<string> workers = new List<string>(new string[] { "workerA", "workerB", "workerC" });
    public Form1() {
      InitializeComponent();
      messageQueuePool = new MessageQueueTaskPool<FtpFile>(new XmlMessageFormatter(new Type[] { typeof(FtpFile) }));
      messageQueuePool.QueueName = "myQueue";
      messageQueuePool.HandleLog = appendLog;
      workers.ForEach(w => messageQueuePool.AddReceiver(w));

      messageQueuePool.OnMessage += MessageQueuePool_OnMessage;
    }

    private void MessageQueuePool_OnMessage(MessageQueueTaskPool<FtpFile>.MessageEventArgs evt) {
      appendLog($"接收到[{evt.name}({evt.id})]数据:{evt.data.fileName} 长度:{evt.data.content.Length}");
    }

    string fileContent = "";

    public class FtpFile {
      public string fileName;
      public string content;
    }


    private void btnPush_Click(object sender, EventArgs e) {
      messageQueuePool.SendMessage(new FtpFile() { content = fileContent, fileName = lblFileName.Text });
    }
    private void button1_Click(object sender, EventArgs e) {
      if (messageQueuePool.IsRuning(workers[0])) {
        MessageBox.Show("拉取已开启");
      } else {
        workers.ForEach(w => messageQueuePool.StartReceive(w));
        btnStart.Text = "拉取中";
        btnStart.Enabled = false;
        btnStop.Visible = true;
      }
    }

    private void btnClearLog_Click(object sender, EventArgs e) {
      this.tbMsg.Clear();
    }

    private void btnStop_Click(object sender, EventArgs e) {
      if (!messageQueuePool.IsRuning(workers[0])) {
        MessageBox.Show("拉取未开启");
      } else {
        workers.ForEach(w => messageQueuePool.StopReceive(w));
        btnStart.Text = "拉取";
        btnStop.Visible = false;
        btnStart.Enabled = true;
      }
    }
    private void appendLog(string msg) {
      if (this.InvokeRequired) {
        if (!this.IsDisposed) {
          this.Invoke(new Action(() => {
            appendLog(msg);
          }));
        }
      } else {
        tbMsg.AppendText(msg + "\r\n");
      }
    }

    private void btnSelectFile_Click(object sender, EventArgs e) {
      if (openFileDialog1.ShowDialog() == DialogResult.OK) {
        tbFilePath.Text = openFileDialog1.FileName;
        var file = new FileInfo(tbFilePath.Text);
        if (file.Exists) {
          lblFileLength.Text = file.Length / 1024 + "KB";
          lblFileName.Text = file.Name;
          using (var stream = file.OpenRead()) {
            using (var read = new StreamReader(stream)) {
              fileContent = read.ReadToEnd();
            }
          }
        } else {
          MessageBox.Show("文件不存在:" + tbFilePath.Text);
        }
      }
    }
  }
}